<template>
  <view class="goodslist_content">
    <view
      @click.stop="goExchangPage(item.id)"
      class="goodcard"
      v-for="item in farmerCommodityList"
      :key="item.id">
      <image
        class="goods_img"
        :src="item.imgUrl"
        alt="" />
      <view class="text">
        <view class="name"
          ><text class="youxuan"> 优选 </text>{{ item.name }}</view
        >
        <view class="tokenPrice">
          <view class="left">
            <text class="red">{{ item.consumeNum }}{{ $tokenName }}</text>
          </view>
          <view class="right1"> 立即兑换 </view>
        </view>
      </view>
    </view>
    <view
      v-if="haMore"
      style="
        text-align: center;
        color: #999;
        padding-bottom: 10rpx;
        height: 50px;
      ">
      正在加载...
    </view>
    <view
      v-else
      style="
        text-align: center;
        color: #999;
        padding-bottom: 10rpx;
        height: 50px;
      ">
      没有更多了
    </view>
  </view>
</template>

<script>
  import { getFarmerCommodityList } from '@/apis/first'
  import { getUserInfo } from '@/utils/auth'
  export default {
    name: 'GoodsList',
    data() {
      return {
        farmerCommodityList: [],
        page: {
          pageNum: 1,
          pageSize: 10
        },
        triggered: false,
        nextPage: 1,
        isEmpty: false,
        haMore: true,
        userInfo: {},
        tokenNum: 0
      }
    },
    onLoad() {},
    onShow() {},
    mounted() {
      this.getFarmerCommodityList()
      // this.getMemberInfo()
    },
    methods: {
      async getFarmerCommodityList(hasMore) {
        // this.haMore = true
        if (hasMore) {
          this.haMore = true
          this.farmerCommodityList = []
          this.page.pageNum = 1
          this.isEmpty = false
        }
        if (!this.haMore) return
        const res = await getFarmerCommodityList({ ...this.page })
        if (res.code === 200) {
          const data = res.data
          // const data = rsaDecrypt(res.data.data)
          // 如果是第一页，则清空数据
          if (this.page.pageNum === 1) {
            this.farmerCommodityList = []
          }
          // 添加数据
          this.farmerCommodityList = [...this.farmerCommodityList, ...data.list]
          // 判断是否为空数组
          this.isEmpty = this.farmerCommodityList.length === 0
          // 计算下一页页码
          this.page.pageNum = ++data.pageNum
          console.log(data)
          // 判断是否还有下一页数据
          this.haMore = this.page.pageNum <= data.pages
          // if (!this.haMore) uni.utils.toast('没有更多数据了')
          console.log(this.farmerCommodityList, 'farmerCommodityList')
        } else {
          // console.log(res)
          // uni.utils.toast('获取列表失败')
        }
      },

      notToken() {
        uni.utils.toast(this.$tokenName + '数量不够')
      },
      goExchangPage(id) {
        this.userInfo = getUserInfo()
        uni.navigateTo({
          url:
            '/pages/exchangeGoods/index?farmerCommodityId=' +
            id +
            '&memberId=' +
            this.userInfo.id
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  .goodslist_content {
    .goodcard {
      height: 240rpx;
      background-color: #fff;
      border-radius: 12rpx;
      margin-bottom: 20rpx;
      padding: 20rpx;
      display: flex;
      .goods_img {
        width: 200rpx;
        border-radius: 12rpx;
        height: 200rpx;
        margin-right: 20rpx;
      }
      .text {
        flex: 1;
        .name {
          margin-bottom: 104rpx;
          font-size: 16px;
          vertical-align: middle;
          font-size: 28rpx;
          .youxuan {
            width: 56rpx;
            height: 40rpx;
            padding: 4rpx;
            margin-right: 20rpx;
            background: linear-gradient(90deg, #34bbff 0%, #3282ff 100%);
            border-radius: 4rpx 4rpx 4rpx 4rpx;
            font-size: 24rpx;
            color: #fffdec;
          }
        }
        .tokenPrice {
          font-size: 40rpx;
          font-weight: 700;
          display: flex;
          .left {
            flex: 1;
          }
          .red {
            color: #ff3d00;
          }
          .right1 {
            font-size: 28rpx;
            text-align: center;
            line-height: 60rpx;
            width: 192rpx;
            height: 60rpx;
            font-weight: bold;
            background: linear-gradient(90deg, #03dbfd 0%, #2f64ff 100%);
            box-shadow: inset 0rpx -8rpx 8rpx 0rpx rgba(241, 240, 238, 0.3);
            border-radius: 30rpx 30rpx 30rpx 30rpx;
            color: #ffffff;
          }
        }
      }
    }
  }
  ::-webkit-scrollbar {
    width: 0;
    height: 0;
    color: transparent;
  }
</style>
